<template>
  <div align="center">

    <el-row>
      <el-col :span="2">
        <router-link :to="{path: '/add',query:{pageNum:pageInfo.pageNum}}"><el-button type="success" >添加按钮</el-button></router-link>
      </el-col>
      <el-col :span="22">
        <el-form :inline="true" :model="search" class="demo-form-inline">
          <el-form-item label="查询 :">
            <el-input v-model="search" placeholder="请输入要查询的内容"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-table :data="emp" border style="width: 100%">

      <el-table-column prop="id"  type="selection" width="120px" align="center"></el-table-column>

      <el-table-column  prop="index" label="序列号" type="index"  width="120px"></el-table-column>

      <el-table-column prop="id"  label="编号" width="120px" ></el-table-column>

      <el-table-column  label="姓名" width="120px">
        <template slot-scope="e">
          <div v-html="e.row.name"></div>
        </template>
      </el-table-column>

      <el-table-column prop="age" label="年龄" width="120px"></el-table-column>

      <el-table-column prop="pir" label="日期" width="120px"></el-table-column>

      <el-table-column  label="内容" width="120px">
        <template slot-scope="e">
          <div v-html="e.row.content"></div>
        </template>
      </el-table-column>

      <el-table-column prop="address" label="地址" width="120px"></el-table-column>


      <el-table-column label="操作" align="center">
        <template slot-scope="s">
          <el-button type="primary"  @click="handleDelete(pageInfo.pageNum,s.row.id)">删除按钮</el-button>&nbsp;<router-link :to="{path:'/update',query:{pageNum:pageInfo.pageNum,id:s.row.id}}"><el-button type="success" >修改按钮</el-button></router-link>
        </template>
      </el-table-column>

    </el-table>

    <el-pagination
      layout="sizes, prev, pager, next, jumper, ->, total"
      :total=this.pageInfo.total
      :page-size.sync="pageSize"
      :page-sizes="pageSizes"
      :current-page.sync="currentPage"
      :pager-count="pagerCount"
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script>
    export default {
        name: "ShowEmp",
      data(){
        return{
          pageInfo:{},
          emp:[],
          search:"",
          total:500,//总共多少条数据
          pageSize:2,//每页显示10条
          pageSizes:[2,5,10,20],//每页显示条数候选值
          currentPage:1,//当前页为第2页
          pagerCount:9,//最多显示9个页码，多余的会折叠
        }
      },
      created() {
        let page = this.$route.query.pg;
        if(page!=null){
          this.handleShow(page,2);
          return;
        }

        this.handleShow(1,2);
      },

      methods: {
        handleShow(i,j){
          this.axios.get("/emp",{
            params:{
              pageNum:i,
              pageSize:j
            }
          }).then(resp=>{
            this.pageInfo = resp.data
            this.emp = resp.data.list
            console.log(this.pageInfo)
          }).catch(error=>{
            console.log(error)
          })
        },

        handleDelete(pageNum,id){
          if(!confirm("你确定要删除吗？")){
            return
          }
          this.axios.delete("/emp/"+id).then(resp=>{
            if(resp.data.status=="success"){
              this.$message.success("删除成功");
              this.handleShow(pageNum,2);
            }else {
              this.$message.success("删除失败");
            }
            console.log(resp.data.status)
          }).catch(error=>{
            console.log(error)
          })
        },

        handleSizeChange(size){
          this.handleShow(this.pageInfo.pageNum,size);
        },
        handleCurrentChange(page){
          this.handleShow(page,this.pageInfo.pageSize);
        },

        onSubmit(){
          alert(this.search);
          this.axios.get("/search/"+this.search).then(resp=>{
            this.emp = resp.data
          }).catch(error=>{
            console.log(error)
          })
        }
      }
    }
</script>

<style scoped>
  div{
    color: hotpink;
  }
</style>
